package com.service.admin.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.product.controller.req.CategoryPageReq;
import com.service.admin.product.service.ProductCategoryService;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.model.ProductCategory;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.dao.mapper.ProductCategoryMapper;


@Service("productCategoryService")
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Override
    public CommonPageRep<ProductCategory> page(CategoryPageReq categoryPageReq) {
        LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(categoryPageReq.getCategoryName())) {
            queryWrapper.like(ProductCategory::getCategoryName, categoryPageReq.getCategoryName());
        }
        if (StringUtils.isNotBlank(categoryPageReq.getLanguage())) {
            queryWrapper.eq(ProductCategory::getLanguage, categoryPageReq.getLanguage());
        }
        queryWrapper.eq(ProductCategory::getCategoryStatus, CommonStatusEnums.ACTIVE.getCode())
                .orderByAsc(ProductCategory::getSort);

        Page<ProductCategory> page = PageHelper.startPage(categoryPageReq.getPageNo(), categoryPageReq.getPageSize());
        list(queryWrapper);
        return CommonPageRep.buildResultRep(page);
    }

    @Override
    public List<ProductCategory> listAll(CategoryPageReq categoryPageReq) {
        LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(categoryPageReq.getLanguage())) {
            queryWrapper.eq(ProductCategory::getLanguage, categoryPageReq.getLanguage());
        }
        queryWrapper.eq(ProductCategory::getCategoryStatus, CommonStatusEnums.ACTIVE.getCode());

        return list(queryWrapper);
    }

}
